package com.qy566.gps.pulsar.admin;

import org.apache.pulsar.client.admin.PulsarAdmin;
import org.apache.pulsar.client.admin.PulsarAdminException;
import org.apache.pulsar.client.api.PulsarClientException;

import java.util.List;

/**
 * @author wangnaiqi
 * @description TODO
 * @date 2024/1/16
 */
public class TopicAdmin {
    public static void main(String[] args) throws PulsarClientException, PulsarAdminException {
        String serviceURL = "http://10.0.0.27:32759";
        PulsarAdmin admin = PulsarAdmin.builder().serviceHttpUrl(serviceURL).build();

//        TopicAdmin topicAdmin = new TopicAdmin();
//        topicAdmin.createTopic(admin,true,"persistent://gpsTenant/positionNamespace/positionTopic",50);


        // 3 列出某个名称空间下，所有的topic
        // 3.1 无分区的Topic
//        System.out.println("无分区的Topic：");
//        List<String> topics = admin.topics().getList("gpsTenant/positionNamespace");
//        for (String topic : topics) {
//            System.out.println(topic);
//        }
        // 3.2 有分区的Topic
//        System.out.println("有分区的Topic：");
//        List<String> topics = admin.topics().getPartitionedTopicList("gpsTenant/positionNamespace");
//        for (String topic : topics) {
//            System.out.println(topic);
//        }

        // 4 更新Topic：增加分区数
//        admin.topics().updatePartitionedTopic("persistent://gpsTenant/positionNamespace/positionTopic",50);
//        int partitions = admin.topics().getPartitionedTopicMetadata("persistent://itcast_pulsar_t/itcast_pulsar_n/my-topic1").partitions;
//        System.out.println("topic的分区数为：" + partitions);

        // 5 删除Topic
        // 5.1 删除没有分区的
//        admin.topics().delete("persistent://itcast_pulsar_t/itcast_pulsar_n/my-topic3");
//        admin.topics().delete("non-persistent://itcast_pulsar_t/itcast_pulsar_n/my-topic4");
        // 5.2 删除有分区的
        admin.topics().deletePartitionedTopic("persistent://gpsTenant/gpsServerNamespace/deductionCallbackTopic");
//        admin.topics().deletePartitionedTopic("non-persistent://itcast_pulsar_t/itcast_pulsar_n/my-topic2");



        admin.close();
    }

    private void createTopic(PulsarAdmin admin,Boolean isPersistent,String topicName) throws PulsarAdminException {
        if (isPersistent) {
            admin.topics().createNonPartitionedTopic(topicName);
        } else {
            admin.topics().createNonPartitionedTopic("non-".concat(topicName));
        }
    }

    private void createTopic(PulsarAdmin admin,Boolean isPersistent,String topicName,int partitions) throws PulsarAdminException {
        if (isPersistent) {
            admin.topics().createPartitionedTopic(topicName,partitions);
        } else {
            admin.topics().createPartitionedTopic("non-".concat(topicName),partitions);
        }
    }
}
